package com.wx.commons;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import com.wx.controller.WxMassMsgController;

/**
 * 描述：定时执行群发消息，耦合性较强，是专用的类<br/>
 * 作者：data-zrb <br/>
 * 修改日期：2015年10月12日 - 上午11:47:36<br/>
 */
public class SentMassJob implements Job {

	private Logger logger = LoggerFactory.getLogger(getClass());
	
	private SchedulerFactoryBean schedulerFactoryBean;
	
	private WxMassMsgController wxMassMsgController;
	
	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException{
		//删除任务
		String name = context.getMergedJobDataMap().getString("name");
		Integer id = context.getMergedJobDataMap().getIntValue("id");
		
		this.schedulerFactoryBean = (SchedulerFactoryBean) context.getMergedJobDataMap().get("schedulerFactoryBean");
		this.wxMassMsgController = (WxMassMsgController) context.getMergedJobDataMap().get("wxMassMsgController");
		String wxid = context.getMergedJobDataMap().getString("wxid");
		String serverPath = context.getMergedJobDataMap().getString("serverPath");
		Scheduler scheduler = schedulerFactoryBean.getScheduler();
		
		//执行
		wxMassMsgController.doTaskSent(id, wxid, serverPath);
		String group = context.getMergedJobDataMap().getString("group");
        JobKey jobKey = JobKey.jobKey(name, group);  
        try{
			scheduler.deleteJob(jobKey);
			logger.info("删除定时任务 [name:"+name+", group:"+group+"]成功！");
		} catch (SchedulerException e){
			logger.error("删除定时任务失败： [name:"+name+", group:"+group+"]");
			e.printStackTrace();
		}  
	}

	
	public void setSchedulerFactoryBean(SchedulerFactoryBean schedulerFactoryBean){
		this.schedulerFactoryBean = schedulerFactoryBean;
	}

	public void setWxMassMsgController(WxMassMsgController wxMassMsgController){
	
		this.wxMassMsgController = wxMassMsgController;
	}
}
